/**
 * Created by zlp on 2018/01/04.
 * For 我的账户
 */
var request = require("superagent");
var moment = require('moment');
var API = require("../support/const");
var requestUtil = require("../common/requestUtil");
var setting = require("../../common/cn/member/index");
var memberSetting = require("../../common/cn/member/index");

let templateResult = {
    datas: {},
    setting: setting
};

//资金记录
exports.fundsRecord = {
    list:function(req,res,next){
        let{page,limit,start_time,end_time,month,funds_type} = req.body;

        if(start_time){
            start_time = start_time +" 00:00:00";
        }
        if(end_time){
            end_time = end_time +" 23:59:59";
        };

        if(month){
            start_time = moment().format('YYYY-MM-DD HH:mm:ss');
            end_time = moment().add(Number(month),'months').format('YYYY-MM-DD HH:mm:ss');
        }
        let header = {
            token: req.cookies.token
        };
        let params ={
            "page_no": page,
            "page_size": limit,
            "order_column":'',
            "order_by":'',
            "condition":{"start_time":start_time,"end_time":end_time,"funds_type":funds_type}
        };
        request.get(API.web.funds.funds_record)
            .query({msg:JSON.stringify(params)})
            .set(header)
            .end(function(_req,_res){
                console.log(_res.text);
                requestUtil.formatTableData(_res).then(function(returnData){
                    res.json(returnData)
                });
            });
    }
};

//利息记录
exports.interest ={
    list:function(req,res,next){
        let{page,limit,start_time,end_time,month} = req.body;

        if(start_time){
            start_time = start_time +" 00:00:00";
        }
        if(end_time){
            end_time = end_time +" 23:59:59";
        };

        if(month){
            start_time = moment().format('YYYY-MM-DD HH:mm:ss');
            end_time = moment().add(Number(month),'months').format('YYYY-MM-DD HH:mm:ss');
        }

        let header = {
            token: req.cookies.token
        };
        let params ={
            "page_no": page,
            "page_size": limit,
            "order_column":'',
            "order_by":'',
            "condition":{"start_time":start_time,"end_time":end_time,"funds_type":3}
        };
        request.get(API.web.funds.funds_profits)
            .query({msg:JSON.stringify(params)})
            .set(header)
            .end(function(_req,_res){
                requestUtil.formatTableData(_res).then(function(returnData){
                    res.json(returnData)
                });
            });
    }
};
//交易记录
exports.order ={
    list:function(req,res,next){
        let{page,limit,start_time,end_time,month,funds_type} = req.body;

        if(start_time){
            start_time = start_time +" 00:00:00";
        }
        if(end_time){
            end_time = end_time +" 23:59:59";
        };

        if(month){
            start_time = moment().format('YYYY-MM-DD HH:mm:ss');
            end_time = moment().add(Number(month),'months').format('YYYY-MM-DD HH:mm:ss');
        }
        let header = {
            token: req.cookies.token
        };
        let params ={
            "page_no": page,
            "page_size": limit,
            "order_column":'',
            "order_by":'',
            "condition":{"start_time":start_time,"end_time":end_time,"funds_type":funds_type}
        };

        request.get(API.web.funds.funds_order)
            .query({msg:JSON.stringify(params)})
            .set(header)
            .end(function(_req,_res){
                requestUtil.formatTableData(_res).then(function(returnData){
                    res.json(returnData)
                });
            });
    }
};

//客户佣金列表
exports.commission ={
    list:function(req,res,next){
        let{page,limit,start_time,end_time,month,funds_type} = req.body;

        if(start_time){
            start_time = start_time +" 00:00:00";
        }
        if(end_time){
            end_time = end_time +" 23:59:59";
        };

        if(month){
            start_time = moment().format('YYYY-MM-DD HH:mm:ss');
            end_time = moment().add(Number(month),'months').format('YYYY-MM-DD HH:mm:ss');
        }

        let header = {
            token: req.cookies.token
        };
        let params ={
            "page_no": page,
            "page_size": limit,
            "order_column":'',
            "order_by":'',
            "condition":{"start_time":start_time,"end_time":end_time,"funds_type":4}
        };
        request.get(API.web.funds.funds_profits)
            .query({msg:JSON.stringify(params)})
            .set(header)
            .end(function(_req,_res){
                requestUtil.formatTableData(_res).then(function(returnData){
                    res.json(returnData)
                });
            });
    }
};

//客户利息列表
exports.interest ={
    list:function(req,res,next){
        let{page,limit,start_time,end_time,month,funds_type} = req.body;

        if(start_time){
            start_time = start_time +" 00:00:00";
        }
        if(end_time){
            end_time = end_time +" 23:59:59";
        };

        if(month){
            start_time = moment().format('YYYY-MM-DD HH:mm:ss');
            end_time = moment().add(Number(month),'months').format('YYYY-MM-DD HH:mm:ss');
        }
        let header = {
            token: req.cookies.token
        };
        let params ={
            "page_no": page,
            "page_size": limit,
            "order_column":'',
            "order_by":'',
            "condition":{"start_time":start_time,"end_time":end_time,"funds_type":funds_type}
        };
        request.get(API.web.funds.funds_profits)
            .query({msg:JSON.stringify(params)})
            .set(header)
            .end(function(_req,_res){
                requestUtil.formatTableData(_res).then(function(returnData){
                    res.json(returnData)
                });
            });
    }
}

//个人资料
exports.userInfo = {
    info:function(req,res,next){
        let header = {
            token: req.cookies.token
        };
        request.get(API.web.personal_center.base_info)
            .set(header)
            .end(function(_req,_res){

                let data = JSON.parse(_res.text);
                data.identity_type = memberSetting.select.identity_type;

                res.json(data);
            });
    },
    //修改登录密码
    updateLoginPasswd:function(req,res,next){
        let header = {
            token: req.cookies.token
        };
        request.put(API.web.personal_center.reset_login_passwd)
            .query({msg:JSON.stringify(req.body)})
            .set(header)
            .end(function(_req,_res){
                requestUtil.formatTableData(_res).then(function(returnData){
                    res.json(returnData)
                });
            });
    },
    //修改支付密码
    updatePayPasswd:function(req,res,next){
        let header = {
            token: req.cookies.token
        };
        request.put(API.web.personal_center.reset_trade_passwd)
            .query({msg:JSON.stringify(req.body)})
            .set(header)
            .end(function(_req,_res){
                requestUtil.formatTableData(_res).then(function(returnData){
                    res.json(returnData)
                });
            });
    },
    //找回支付(交易)密码
    tradeRetrieve:function(req,res,next){
        console.log(req.body);
        request.put(API.web.password.trade_retrieve)
            .query({msg:JSON.stringify(req.body)})
            .end(function(_req,_res){
                console.log(_res.text);
                requestUtil.formatTableData(_res).then(function(returnData){
                    res.json(returnData)
                });
            });
    },
    //重置支付(交易)密码
    tradeRetrieveReset:function(req,res,next){
        request.put(API.web.password.trade_retrieve_reset)
            .query({msg:JSON.stringify(req.body)})
            .end(function(_req,_res){
                console.log(_res.text);
                requestUtil.formatTableData(_res).then(function(returnData){
                    res.json(returnData)
                });
            });
    }
};

//银行卡资料
exports.bankcard ={
    list:function(req,res,next){
        let header = {
            token: req.cookies.token
        };
        request.get(API.web.personal_center.bank_cards)
            .set(header)
            .end(function(_req,_res){
                requestUtil.formatTableData(_res).then(function(returnData){

                    res.json(returnData)
                });
            });
    },

    //根据id 获取详情
    info:function(req,res,next){
        let{id} = req.body;
        let header = {
            token: req.cookies.token
        };

        request.get(API.web.personal_center.bank_cards + "/" + id)
            //.query({msg:JSON.stringify(params)})
            .set(header)
            .end(function(_req,_res){
                res.json(JSON.parse(_res.text));
            });
    },
    //添加银行卡
    add:function(req,res,next){
        let header = {
            token: req.cookies.token
        };
        let params = req.body;
        request.post(API.web.personal_center.bank_cards)
            .query({msg:JSON.stringify(params)})
            .set(header)
            .end(function(_req,_res){
                res.json(JSON.parse(_res.text));
            });
    },
    //修改银行卡
    update:function(req,res,next){
        let header = {
            token: req.cookies.token
        };
        let params = req.body;
        console.log(JSON.stringify(params));
        console.log(API.web.personal_center.bank_cards);
        request.put(API.web.personal_center.bank_cards)
            .query({msg:JSON.stringify(params)})
            .set(header)
            .end(function(_req,_res){
                console.log(_res.text);
                requestUtil.formatTableData(_res).then(function(returnData){
                    res.json(returnData)
                });
            });
    },
    //解绑银行卡
    delete:function(req,res,next){
        let header = {
            token: req.cookies.token
        };
        let params = req.body;
        request.delete(API.web.personal_center.bank_cards + "/" + params.id)
            .query({msg:JSON.stringify(params)})
            .set(header)
            .end(function(_req,_res){
                requestUtil.formatTableData(_res).then(function(returnData){
                    res.json(returnData)
                });
            });
    }

};

//我的合伙人(下级)
exports.child ={
    list:function(req,res,next){
        let{page,limit} = req.body;
        let header = {
            token: req.cookies.token
        };
        let params ={
            "page_no": page,
            "page_size": limit,
            "order_column":'',
            "order_by":''
        };
        request.get(API.web.personal_center.parters)
            .query({msg:JSON.stringify(params)})
            .set(header)
            .end(function(_req,_res){
                requestUtil.formatTableData(_res).then(function(returnData){
                    res.json(returnData)
                });
            });
    }
}

